package com.example.demo01.Impl.webImpl;

import com.example.demo01.Dao.webDao.HealthInfoRecordDao;
import com.example.demo01.Entity.webEntity.HealthInfoRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Repository
public class HealthInfoRecordDaoImpl implements HealthInfoRecordDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> getAllHealthRecords() {
        String sqlQuery="SELECT * FROM healthinforecords";
        return jdbcTemplate.queryForList(sqlQuery);
    }

    @Override
    public List<Map<String, Object>> getHealthRecordByIdNum(String idNum, int limitCount) {
        String sqlQuery="SELECT * FROM healthinforecords WHERE healthinforecords.idnumber=? ORDER BY healthinforecords.loadDate DESC LIMIT ?";
        return jdbcTemplate.queryForList(sqlQuery,new Object[]{idNum,limitCount})
                ;
    }

    /**
     * 根据身份证号获取单条——统计性健康信息记录
     * */
    @Override
    public List<List>  getHealthRecordByIdNum(String idnum) {
        String sql="SELECT idnumber,diastolicPressure,systolicPressure,pluseVal,temperatureVal,breathingRate,loadDate\n" +
                "FROM healthinforecords WHERE idnumber=? ORDER BY loadDate";
        List<HealthInfoRecord> healthInfoRecordList = jdbcTemplate.query(sql, new Object[]{idnum}, new RowMapper<HealthInfoRecord>() {
            @Nullable
            @Override
            public HealthInfoRecord mapRow(ResultSet resultSet, int i) throws SQLException {
                return new HealthInfoRecord(
                        resultSet.getString(1),
                        resultSet.getInt(2),
                        resultSet.getInt(3),
                        resultSet.getInt(4),
                        resultSet.getFloat(5),
                        resultSet.getInt(6),
                        resultSet.getDate(7)
                );
            }
        });
        return changeHealthRecordEntityIntoList(healthInfoRecordList);
    }

    /**
     * 将查询的结果封装成为List集合返回
     * */
    public List<List> changeHealthRecordEntityIntoList(List<HealthInfoRecord> list){
        List<List> resList=new ArrayList<>();
        List<String> fieldNameList=new ArrayList<>(); //字段属性名称
        List<List> valueList=new ArrayList<>();//字段值列表

        //元素值List
        List<Date> dataList=new ArrayList<>();//日期
        List<Float> pressureList=new ArrayList<>();//血压值
        List<Float> pluseList=new ArrayList<>();//脉搏频率
        List<Float> tempatureList=new ArrayList<>();//体温值
        List<Float> breatheList=new ArrayList<>();//呼吸速率
        //添加字段名称
        fieldNameList.add("日期");
        fieldNameList.add("血压值");
        fieldNameList.add("脉搏频率");
        fieldNameList.add("体温值");
        fieldNameList.add("呼吸速率");

        //遍历集合，获取元素值
        list.forEach(new Consumer<HealthInfoRecord>() {
            @Override
            public void accept(HealthInfoRecord healthInfoRecord) {
                dataList.add(healthInfoRecord.getLoadDate());
                pressureList.add(((float)(healthInfoRecord.getSystolicPressure() + healthInfoRecord.getDiastolicPressure()))/2);
                pluseList.add((float)healthInfoRecord.getPluseVal());
                tempatureList.add(healthInfoRecord.getTemperatureVal());
                breatheList.add((float)healthInfoRecord.getBreathingRate());
            }
        });
        //将结果集封装到新的ValueList集合中(5个元素)
        valueList.add(dataList);
        valueList.add(pressureList);
        valueList.add(pluseList);
        valueList.add(tempatureList);
        valueList.add(breatheList);

        //添加字段名称至结果集中
        resList.add(fieldNameList);
        resList.add(valueList);

        return resList;
    }

}
